package com.example.hissys.service;

import com.example.hissys.entity.User;
import com.example.hissys.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户服务层
 * @author Your Name
 * @since 2025-01-03
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private UserMapper userMapper;

    // 用户注册
    public boolean register(User user) {
        // 检查用户名是否存在
        if (userMapper.checkUsernameExists(user.getUsername()) > 0) {
            throw new RuntimeException("用户名已存在");
        }
        // 注册用户
        return userMapper.register(user) > 0;
    }

    // 用户登录
    public User login(String username, String password) {
        User user = userMapper.login(username, password);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        return user;
    }

    /**
     * 根据用户名查询用户ID
     * @param username 用户名
     * @return 用户ID
     */
    public Integer getUserIdByUsername(String username) {
        log.info("Fetching user ID for username: {}", username);
        return userMapper.findIdByUsername(username);
    }
}
